﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace NetworkEasyShare
{
    public class infoMessage
    {
        private string mIPaddr;
        private string mMACaddr;
        private string mNickName;
        private string mStatus;
        

        /// <summary>
        /// Constructor
        /// </summary>
        public infoMessage() { 
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="nickName">nickname</param>
        /// <param name="status">Status</param>
        /// <param name="ipAddr">IP Address</param>
        /// <param name="macAddr">MAC Address</param>
        public infoMessage(string nickName, string status, string ipAddr, string macAddr)
        {
            mNickName = nickName;
            mStatus = status;

            if (macAddr != null || ipAddr != null)
            {
                mIPaddr = ipAddr;
                mMACaddr = macAddr;
            }
        }

        public Stream string2Stream(string str) { 
            byte[] byteArray = Encoding.ASCII.GetBytes(str); 
            MemoryStream stream = new MemoryStream(byteArray);

            return stream;
        }

        /// <summary>
        /// Get message from XML
        /// </summary>
        /// <returns>Message</returns>
        public void fromXml(Stream stream)
        {
            try {
                XmlDocument doc = new XmlDocument();
                doc.Load(stream);
                XmlNode messageNode = doc.SelectSingleNode("msg");

                if(messageNode.Attributes["version"] != null) {
                    if(messageNode.Attributes["version"].InnerText == "1.0.0" && messageNode.Attributes["type"].InnerText == "infoMessage")
                    {
                        mNickName = messageNode.SelectSingleNode("nickName").InnerText;
                        mStatus = messageNode.SelectSingleNode("status").InnerText;
                    }
                }

                stream.Dispose();
            }
            catch (Exception)
            {
                throw new InvalidDataException();
            }
        }

        /// <summary>
        /// Create XML message
        /// </summary>
        /// <returns>Message as XML</returns>
        public string ToXml()
        {
            StringBuilder xmlBuilder = new StringBuilder();
            XmlWriter writter = XmlWriter.Create(xmlBuilder);

            writter.WriteStartDocument();
            writter.WriteStartElement("msg");
                writter.WriteAttributeString("version", "1.0.0");
                writter.WriteAttributeString("type", "infoMessage");
                writter.WriteElementString("nickName", mNickName);
                writter.WriteElementString("status", mStatus);
            writter.WriteEndElement();
            writter.WriteEndDocument();

            writter.Flush();
            writter.Close();

            return xmlBuilder.ToString();
        }

        /// <summary>
        /// Get IP address
        /// </summary>
        public string IPaddr
        { get { return mIPaddr; } }

        /// <summary>
        /// Get MAC address
        /// </summary>
        public string MACaddr
        { get { return mMACaddr; } }

        /// <summary>
        /// Get nickname
        /// </summary>
        public string NickName
        { get { return mNickName; } }

        /// <summary>
        /// Get status
        /// </summary>
        public string Status
        { get { return mStatus; } }
    }
}
